package com.Day5;

import com.Day4.MyStack;

public class stackIssue {


    public boolean isValid(String s) {
        char[] c = s.toCharArray();

        MyStack stack = new MyStack();

        char First = s.charAt(0);
        if(s.length() % 2 != 0){
            return false;
        }//若括号数量为奇数则直接错误

        if(First == ')' || First == ']' || First == '}'){
            return false;
        }//若第一个元素未右括号则直接错误

        for(int i = 0; i < s.length(); i++){
            char in = s.charAt(i);

            if(in == '(' || in == '[' || in == '{'){
                stack.push(in);
            }
            else{
                char out = Character.highSurrogate(stack.pop());//获取栈顶

                switch (in){
                    case '(':
                        return out == ')';
                    case '[':
                        return out == ']';
                    case '{':
                        return out == '}';
                    default:
                        return false;
                }
            }
        }
        return stack.isEmpty();
    }
}
